﻿using System;
using System.IO;
using System.Text.Json;

namespace AutoControlSpectrum.Model
{
    /// <summary>
    /// 程序配置
    /// </summary>
    public class AppSettings
    {
        /// <summary>
        /// 背景扫描等待时长(秒)
        /// </summary>
        public int Bjsmddsc { get; set; } = 10;

        /// <summary>
        /// 选择样品等待时长(秒)
        /// </summary>
        public int Szypddsc { get; set; } = 5;

        /// <summary>
        /// 计算等待时长(秒)
        /// </summary>
        public int Jsddsc { get; set; } = 6;

        /// <summary>
        /// 宏id
        /// </summary>
        public string MacroId { get; set; } = string.Empty;

        private static readonly string ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");

        // 存储唯一的实例
        private static AppSettings _instance;

        // 锁对象，用于线程安全
        private static readonly object _lock = new object();

        // 私有构造函数，防止外部实例化
        private AppSettings(int bjsmddsc = 10, int szypddsc = 5, int jsddsc = 6)
        {
            // 如果配置文件存在，加载配置
            if (File.Exists(ConfigFilePath))
            {
                var loadedSettings = LoadFromLocalFile(ConfigFilePath);
                if (loadedSettings != null)
                {
                    // 增加字段必须给赋值
                    Bjsmddsc = loadedSettings.Bjsmddsc;
                    Szypddsc = loadedSettings.Szypddsc;
                    Jsddsc = loadedSettings.Jsddsc;
                    MacroId = loadedSettings.MacroId;
                }
            }
            else
            {
                // 如果文件不存在，使用默认值
                Bjsmddsc = bjsmddsc;
                Szypddsc = szypddsc;
                Jsddsc = jsddsc;
            }
        }

        public AppSettings()
        { }

        /// <summary>
        /// 获取唯一的实例
        /// </summary>
        public static AppSettings Instance
        {
            get
            {
                // 确保线程安全，只有一个线程可以初始化实例
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = new AppSettings(10);
                    }
                    return _instance;
                }
            }
        }

        /// <summary>
        /// 保存设置到本地文件
        /// </summary>
        public void SaveToLocalFile()
        {
            try
            {
                // 将对象序列化为 JSON 格式
                string jsonString = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });

                // 保存 JSON 到文件
                File.WriteAllText(ConfigFilePath, jsonString);
                Console.WriteLine("Settings saved successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error saving settings: {ex.Message}");
            }
        }

        /// <summary>
        /// 从文件加载设置
        /// </summary>
        private static AppSettings LoadFromLocalFile(string filePath)
        {
            try
            {
                // 读取文件中的 JSON 数据
                string jsonString = File.ReadAllText(filePath);

                // 将 JSON 反序列化为对象
                AppSettings settings = JsonSerializer.Deserialize<AppSettings>(jsonString);
                return settings;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error loading settings: {ex.Message}");
                return null;
            }
        }
    }
}